<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\request;

//银行卡
class Bankcards extends Base{
    public function index(){
        $user = $this->userInformation();
        if($user['RNA']!=1) return  "<script>alert('实名认证通过才能进行该操作');window.location.href='".url('Certification/index')."'</script>";
        $bank = Db::name('user_bank')->where('uid='.Session::get('uid').' and state=0')->select();
        $banks= Db::name('banks')->where('state=1')->select();
        
        $this->assign('banks',$banks);
        $this->assign('user',$user);
        $this->assign('bank',$bank);
        return $this->fetch('Bankcards/index');
    }
    //删除绑定
    public function delbank(){
        $param = $this->request->post();
        $uid = Session::get('uid');
        $bank = Db::name('user_bank')->where(['uid'=>$uid,'id'=>$param['id']])->find();
        // return json_encode($bank);
        if($bank){
            $del = Db::name('user_bank')->where(['uid'=>$uid,'id'=>$param['id']])->update(['state'=>2]);
            if($del){
                return ['msg'=>'success'];
            }else{
                return ['msg'=>'删除失败'];
            }
        }else{
            return ['msg'=>'不存在'];
        }
    }

    //添加卡
    public function addbank(){
        $uid = Session::get('uid');
        $phonenumber = Session::get('phonenumber');
        $param = myarr($this->request->post());
        $user = $this->userInformation();
        if($user['RNA']!=1) ['msg'=>'未实名认证不能添加'];
        $cer = Db::name('user_certification')->where(['uid'=>$uid,'status'=>1])->find();
        if(!$cer){
            $cer['IDnumber'] = '';
        }
        $option = $param['option'];
        if($option==0){
        if($param['zfb']==null) return ['msg'=>'账号不能为空'];
        $zfb = $param['zfb'];
        $zfb2 = $param['zfb2'];
        if(preg_match("/[\',:;*?~`!#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$zfb)) return ['msg'=>'支付宝格式错误'];
        if($zfb!=$zfb2) return ['msg'=>'支付宝账户不一致'];
                //是不是已经存在
                $banks = Db::name('user_bank')->where(['uid'=>$uid,'bankCard'=>$zfb,'state'=>0])->find();
                if($banks) return ['msg'=>'账号已经存在'];
                if(Session::get('codes')!=$param['codes']) return ['msg'=>'资料已经提交'];
                Session::set('codes',mt_rand(1000,9999));
                $data = [
                    'uid' => $uid,
                    'bankName'=>'支付宝',
                    'bankCard'=>$zfb,
                    'name'=>$user['name'],
                    'IDnumber'=>$cer['IDnumber'],
                    'AccountType'=>2,
                    'bankPhone'=>isset($user['phonenumber'])?$user['phonenumber']:'',
                    'createtime'=>date('Y-m-d H:i:s')
                ];
                $in = Db::name('user_bank')->insert($data);
            //添加记录
            $ins = Db::name('login_record')->insert([
                    'uid'=>Session::get('uid'),
                    'logintoip'=>$_SERVER['REMOTE_ADDR'],
                    'logintime'=>date('Y-m-d H:i:s'),
                    'operation'=>'添加支付宝账号']);
                if($in){
                    return ['msg'=>'success'];
                }else{
                    return ['msg'=>'添加失败'];
                }
        }else{
            if(preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $param['bankCard']) != 1) return ['msg'=>'银行卡格式错误'];
            if(preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $param['bankCards']) !=1) return ['msg'=>'银行卡格式错误'];
            $b = Db::name('user_bank')
                ->where(['uid'=>$user['id'],'bankCard'=>$param['bankCard'],'state'=>0])
                ->find();
            if($b) return ['msg'=>'此卡已经绑定'];
            if($param['bankCard']==$param['bankCards']){
                $data = [
                    'uid' => $uid,
                    'bankName'=>htmlspecialchars($param['bankName']),
                    'bankCard'=>$param['bankCard'],
                    'bankType'=>htmlspecialchars($param['sheng'].$param['shi'].$param['qu'].$param['bankTypes']),
                    'bankPhone'=>isset($user['phonenumber'])?$user['phonenumber']:'',
                    'name'=>$user['name'],
                    'IDnumber'=>$cer['IDnumber'],
                    'AccountType'=>1,
                    'createtime'=>date('Y-m-d H:s:i'),
                ];
                if(preg_match('/select|insert|update|CR|document|LF|eval|delete|script|alert|\'|\/\*|\#|\--|\ --|\/|\*|\-|\+|\=|\~|\*@|\*!|\$|\%|\^|\&|\(|\)|\/|\/\/|\.\.\/|\.\/|union|into|load_file|outfile/', $data['bankType']) == 1) return ['msg'=>'信息格式错误'];
                if(Session::get('codes')!=$param['codes']) return ['msg'=>'资料已经提交'];
                Session::set('codes',mt_rand(1000,9999));
                $in = Db::name('user_bank')->insert($data);
                //添加记录
                $ins = Db::name('login_record')->insert([
                    'uid'=>Session::get('uid'),
                    'logintoip'=>$_SERVER['REMOTE_ADDR'],
                    'logintime'=>date('Y-m-d H:i:s'),
                    'operation'=>'添加银行卡账号']);
                if($in){
                    return ['msg'=>'success'];
                }else{
                    return ['msg'=>'添加失败'];
                }
            }else{
                return ['msg'=>'账号不一致'];
            }
        }




    }
}